package com.yiyuan.app.service.impl;

import com.guaike.core.entity.param.Page;
import com.guaike.core.entity.result.PageEntity;
import com.guaike.core.entity.result.Result;
import com.guaike.core.redis.context.ApiContext;
import com.guaike.core.utils.ListUtil;
import com.yiyuan.app.dao.DDetailsPineconeDAO;
import com.yiyuan.app.dao.GRecordGameDAO;
import com.yiyuan.app.dao.GRecordPrizeDAO;
import com.yiyuan.app.entity.po.DDetailsPineconePO;
import com.yiyuan.app.entity.po.GRecordGamePO;
import com.yiyuan.app.entity.po.GRecordPrizePO;
import com.yiyuan.app.entity.result.AppUser;
import com.yiyuan.app.entity.result.RecordGameVO;
import com.yiyuan.app.entity.result.RecordPineconeVO;
import com.yiyuan.app.entity.result.RecordPrizeVO;
import com.yiyuan.app.service.RecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class RecordServiceImpl implements RecordService {

    private final GRecordGameDAO gRecordGameDAO;
    private final DDetailsPineconeDAO dDetailsPineconeDAO;
    private final GRecordPrizeDAO gRecordPrizeDAO;

    @Override
    public Result<PageEntity<RecordGameVO>> myGamePage(Page param) {
        AppUser currentUser = ApiContext.getCurrentUser();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<GRecordGamePO> poPage = gRecordGameDAO.findHistoryPage(currentUser.getUserId(), param.getPageIndex(), param.getPageSize());
        List<RecordGameVO> vos = ListUtil.copyList(poPage.getRecords(), RecordGameVO.class);
        PageEntity<RecordGameVO> result = new PageEntity<>();
        result.setTotal(poPage.getTotal());
        result.setList(vos);
        return Result.success().data(result).build();
    }

    @Override
    public Result<PageEntity<RecordPineconeVO>> myPineconePage(Page param) {
        AppUser currentUser = ApiContext.getCurrentUser();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<DDetailsPineconePO> poPage = dDetailsPineconeDAO.findPage(currentUser.getUserId(), param.getPageIndex(), param.getPageSize());
        List<RecordPineconeVO> vos = ListUtil.copyList(poPage.getRecords(), RecordPineconeVO.class);
        PageEntity<RecordPineconeVO> result = new PageEntity<>();
        result.setTotal(poPage.getTotal());
        result.setList(vos);
        return Result.success().data(result).build();
    }

    @Override
    public Result<PageEntity<RecordPrizeVO>> myPrizePage(Page param) {
        AppUser currentUser = ApiContext.getCurrentUser();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<GRecordPrizePO> poPage = gRecordPrizeDAO.findPage(currentUser.getUserId(), param.getPageIndex(), param.getPageSize());
        List<RecordPrizeVO> vos = new ArrayList<>();
        poPage.getRecords().forEach(po -> {
            RecordPrizeVO vo = new RecordPrizeVO();
            BeanUtils.copyProperties(po, vo);
            vo.setPrizePinecone(po.getPrizePinecone().longValue());
            vos.add(vo);
        });
        PageEntity<RecordPrizeVO> result = new PageEntity<>();
        result.setTotal(poPage.getTotal());
        result.setList(vos);
        return Result.success().data(result).build();
    }
}
